package com.demo.paypalmanagement.controller.admin;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.paypalmanagement.business.OrderBusinessService;
import com.demo.paypalmanagement.common.BuildQuery;
import com.demo.paypalmanagement.common.EmailUtils;
import com.demo.paypalmanagement.common.ResultMsg;
import com.demo.paypalmanagement.config.response.BizException;
import com.demo.paypalmanagement.convert.OrderConvert;
import com.demo.paypalmanagement.model.entity.Order;
import com.demo.paypalmanagement.model.vo.OrderVO;
import com.demo.paypalmanagement.model.vo.PayVO;
import com.demo.paypalmanagement.model.vo.PaypalVO;
import com.demo.paypalmanagement.model.vo.Result;
import com.demo.paypalmanagement.service.IOrderService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.io.IOException;
import java.net.BindException;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author dayu
 * @since 2024-12-02
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private OrderBusinessService orderBusinessService;

    @GetMapping("/page")
    public IPage<Order> getOrderPage(OrderVO vo) {
        if (ObjectUtils.isEmpty(vo)) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }

        Page<Order> page = new Page<>(vo.getPageNo(), vo.getPageSize());

        return orderService.page(page, BuildQuery.buildOrderQuery(vo));
    }

    @GetMapping("/remove")
    public Result removeOrder(OrderVO vo) {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getOrderId())) {
            throw new BizException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        vo.setDeleted(1);
        return Result.success(orderService.saveOrUpdate(OrderConvert.INSTANCE.convertVOToEntity(vo)));
    }

    @PostMapping("/pay")
    public Result pay(@RequestBody PayVO vo) throws IOException {
        return Result.success(orderBusinessService.payOrder(vo));
    }

    @GetMapping("/paySuccess")
    public RedirectView paySuccess(PaypalVO data) throws IOException {
        if (ObjectUtils.isEmpty(data) || StringUtils.isBlank(data.getToken())) {
            return new RedirectView("/error.html");
        }
        Boolean b =  orderBusinessService.confirmOrder(data);
        return b?new RedirectView("/pay-success.html"):new RedirectView("/error.html");
    }

    @GetMapping("/refund")
    public Result refund(OrderVO vo) throws IOException {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getPayOrderNo())) {
            throw new BindException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        Boolean b =  orderBusinessService.refund(vo);
        return b?Result.success():Result.fail(400,"不符合退款要求");
    }

    @GetMapping("/applyRefund")
    public Result applyRefund(OrderVO vo) throws IOException {
        Order byId = orderService.getById(vo.getOrderId());
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getOrderId()) || ObjectUtils.isEmpty(byId)) {
            throw new BindException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        if (byId.getStatus().equals(3) || byId.getStatus().equals(4) || byId.getStatus().equals(1)) {
            throw new BindException(ResultMsg.ORDER_CLOSE.getMsg());
        }
        byId.setStatus(6);
        return Result.success(orderService.updateById(byId));
    }

    @GetMapping("/get")
    public Result get(OrderVO vo) throws IOException {
        if (ObjectUtils.isEmpty(vo) || ObjectUtils.isEmpty(vo.getOrderId())) {
            throw new BindException(ResultMsg.OBJECT_IS_EMPTY.getMsg());
        }
        return Result.success(orderService.getById(vo.getOrderId()));
    }




}
